package repo

import (
	"fmt"

	"github.com/pkg/errors"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var db *gorm.DB

type GenOrm string

var (
	GenOrmGorm  GenOrm = "gorm"
	GenOrmBeego GenOrm = "beego"
)

type MysqlCfg struct {
	Dsn    string
	Db     string
	Tables []string
	Orm    GenOrm
}

func NewDB(cfg *MysqlCfg) {
	dbins, err := gorm.Open(mysql.Open(cfg.Dsn), nil)
	if err != nil {
		// 	panic(errors.Wrap(err, fmt.Sprintf("指定的dsn不合法：%s", cfg.Dsn)))
		// }
		// if err = db.DB; err != nil {
		panic(errors.Wrap(err, fmt.Sprintf("数据库建立连接出错，请检查连接字符串 '%s'", cfg.Dsn)))
	} else {
		db = dbins
	}
}

func Ins() *gorm.DB {
	return db
}
